import comTitle from "@/views/com/com_title.vue";
import cropperModal from "@/components/cropper/index.vue";
import QuillEditor from "@/components/Editor/QuillEditor.vue";
import { fileBase64, toDecimal2 } from '@/utils/util'
export default {
    components: { comTitle, cropperModal, QuillEditor },
    data() {
        return {
            labelWidth: "120px",
            disabled: false,
            ruleForm: {
                sptm: 0,
                kcsl: 1,
                cxsp: 0,
                mdsp: 0,
                xsqd: 0,
                kchs: [{}],
                cxsj: [] //促销时间集合
            },
            soldOut: true,  //如初始库存为0，商品上架后为售罄状态
            rules: {
                name: [
                    { required: true, },
                ],
                spmc: [
                    { required: true },
                ],
                sptm: [
                    { required: true },
                ],
                sptm1: [
                    { required: true },
                ],
            },
            gysmc: [                //供应商名称
                {
                    label: "小鸟伏特加",
                    value: 1
                }
            ],
            wdList: [                //温度
                {
                    label: "常温",
                    value: 1
                }, {
                    label: "热",
                    value: 2
                }, {
                    label: "去冰",
                    value: 3
                }, {
                    label: "少冰",
                    value: 4
                }
            ],
            tdList: [                //糖度
                {
                    label: "无糖",
                    value: 1
                }, {
                    label: "七分糖",
                    value: 2
                }, {
                    label: "全糖",
                    value: 3
                },
            ],
            tqList: [
                {
                    label: "通勤",
                    value: 1
                }, {
                    label: "居家",
                    value: 2
                },
            ],
            jjList: [
                {
                    label: "修身",
                    value: 1
                }, {
                    label: "中长款",
                    value: 2
                }, {
                    label: "气质",
                    value: 3
                },
            ],
            mendianList: [  //门店list
                {
                    value: 1,
                    label: '东南',
                    children: [{
                        value: 2,
                        label: '上海',
                        children: [
                            { value: 3, label: '普陀' },
                            { value: 4, label: '黄埔' },
                            { value: 5, label: '徐汇' }
                        ]
                    }, {
                        value: 7,
                        label: '江苏',
                        children: [
                            { value: 8, label: '南京' },
                            { value: 9, label: '苏州' },
                            { value: 10, label: '无锡' }
                        ]
                    }, {
                        value: 12,
                        label: '浙江',
                        children: [
                            { value: 13, label: '杭州' },
                            { value: 14, label: '宁波' },
                            { value: 15, label: '嘉兴' }
                        ]
                    }]
                }, {
                    value: 17,
                    label: '西北',
                    children: [{
                        value: 18,
                        label: '陕西',
                        children: [
                            { value: 19, label: '西安' },
                            { value: 20, label: '延安' }
                        ]
                    }, {
                        value: 21,
                        label: '新疆维吾尔族自治区',
                        children: [
                            { value: 22, label: '乌鲁木齐' },
                            { value: 23, label: '克拉玛依' }
                        ]
                    }]
                }
            ],
            sptmPlaceholder: false,  //商品条码
            imageUrl: "",
            loading: false,  //上传
            optionsData: {
                title: "商品图片",     //弹框表头
                img: '',   //裁切图片地址
                autoCrop: true, //是否默认生成截图框
                autoCropWidth: 200, //默认生成截图框宽度
                autoCropHeight: 200, //默认生成截图框高度
                fixedBox: true //固定截图框大小 不允许改变
            },
            number: 0,
            fileList: [],
            fileListUrl: [], //放大展示
        }
    },
    methods: {
        sptmChange(val) {
            if (val) {
                this.sptmPlaceholder = true
            } else {
                this.sptmPlaceholder = false
            }
        },
        submitSave() {   //提交并保存
            console.log(this.ruleForm)
            if (this.ruleForm.sptm == 0) {
                this.rules.sptm1[0].required = false;  //商品条码
            } else {
                this.rules.sptm1[0].required = true;
            }
            this.$refs['ruleForm'].validate((valid) => {
                if (valid) {
                    alert('submit!');
                } else {
                    console.log('error submit!!');
                    return false;
                }
            });
        },
        save() {   //保存
            console.log(this.ruleForm)
            if (this.ruleForm.sptm == 0) {
                this.rules.sptm1[0].required = false; //商品条码
            } else {
                this.rules.sptm1[0].required = true;
            }
            this.$refs['ruleForm'].validate((valid) => {
                if (valid) {
                    alert('submit!');
                } else {
                    console.log('error submit!!');
                    return false;
                }
            });
        },
        quxiao(ruleForm) {  //取消
            this.$router.go(-1)
            this.$refs[ruleForm].resetFields();
            this.ruleForm = {
                sptm: 0,
                kcsl: 1,
                cxsp: 0,
                mdsp: 0
            }
        },

        handleChange(info) {  //上传触发
            let { options } = this;
            console.log(info)
            fileBase64(info.file.originFileObj).then(result => {
                let target = Object.assign({}, options, {
                    img: result.result,
                    files: result.files
                })
                this.$refs['cropperModal'].edit(target);
            })
        },
        beforeUpload(file) {
            const isJpgOrPng = file.type === 'image/jpeg' || file.type === 'image/png';
            if (!isJpgOrPng) {
                this.$message.error('图片格式不支持！');
            }
            const isLt1M = file.size / 1024 / 1024 < 1;
            if (!isLt1M) {
                this.$message.error('图片大小限制在1MB内!');
            }
            return isJpgOrPng && isLt1M;
        },
        setZhutu() {

        },
        delUplod(file) {
            const index = this.fileList.indexOf(file)
            const newFileList = this.fileList.slice()
            newFileList.splice(index, 1)
            this.fileList = newFileList
        },
        handleCropperSuccess(data) {  //图片裁切完成保存
            fileBase64(data).then(result => {
                this.imageUrl = result.result
                this.fileListUrl.push(this.imageUrl)
                this.fileList.push({
                    uid: this.number++,
                    name: 'image.png',
                    status: 'done',
                    url: this.imageUrl
                })
            })
        },
        toDecimalBlur(name) {  //保留两位小数
            if (name === 'xsj') {
                this.ruleForm.xsj = toDecimal2(this.ruleForm.xsj)
            } else if (name === 'cbj') {
                this.ruleForm.cbj = toDecimal2(this.ruleForm.cbj)
            } else if (name === 'kcsl') {
                console.log()
                if (this.ruleForm.kcsl < 0) {
                    this.ruleForm.kcsl = 0
                }
            }

        },
        editorChange(quill, html, text) {  //富文本编辑器
            console.log(quill)
            console.log(html)
            console.log(text)
            this.ruleForm.spxq = html
        },
        addKchs() {    //库存换算新增
            this.ruleForm.kchs.push({});
        },
        editKchs(item) {  //库存换算删除
            let index = this.ruleForm.kchs.indexOf(item);
            if (index !== -1) {
                this.ruleForm.kchs.splice(index, 1);
            }
        },
        cxsjChange(e) {  //促销时间
            var timeList = [this.formDate(e[0]._d), this.formDate(e[1]._d)]
            this.ruleForm.startCxsj = timeList[0]
            this.ruleForm.endCxsj = timeList[1]
        },
    },

} 